<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElNotification } from 'element-plus'
//引入获取当前时间的函数
import { getTime } from '@/utils/time'
//引入用户相关的小仓库
import useUserStore from '../../store/modules/user'
let useStore = useUserStore()
//获取el-form组件
let loginFormRef = ref()
//获取路由器
let $router = useRouter()
let $route = useRoute()
//定义变量控制按钮加载效果
let loading = ref(false)
//收集账号和密码
let loginForm = reactive({ username: '', password: '' })
//登录按钮回调
const login = async () => {
  //保证全部表单项校验通过才发请求
  //判断路由是否携带query参数，如果有，则登录成功后跳转到query参数指定的路由，如果没有，则跳转到首页
  let redirect: any = $route.query.redirect

  let result = loginFormRef.value.validate()
  console.log(result)
  loading.value = true
  //通知仓库发请求
  //请求成功->首页展示数据
  //请求失败->提示错误信息
  try {
    //保证登录成功
    await useStore.userLogin(loginForm)
    //编程式导航跳转到数据首页
    // $router.push('/')
    $router.push({ path: redirect || '/' })
    //登录成功的提示信息
    ElNotification({
      title: '成功',
      message: `hi,${getTime()}`,
      type: 'success',
    })
    //登录成功加载效果消失
    loading.value = false
  } catch (error) {
    //登录失败加载效果消失
    loading.value = false
    //登录失败的提示信息
    ElNotification({
      title: '失败',
      message: (error as Error).message,
      type: 'error',
    })
  }
}
//表单校验需要的配置对象
const rules = {
  username: [
    {
      required: true,
      message: '用户名不能为空',
      trigger: 'blur',
    },
    {
      min: 5,
      max: 10,
      message: '用户名必须在6-10位之间',
      trigger: 'blur',
    },
  ],
  password: [
    {
      required: true,
      message: '密码不能为空',
      trigger: 'blur',
    },
    {
      min: 6,
      max: 15,
      message: '用户名必须在6-15位之间',
      trigger: 'blur',
    },
  ],
}
</script>
<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0">占位的文字</el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginFormRef"
        >
          <h1>Hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              :prefix-icon="Lock"
              v-model="loginForm.password"
              show-password
              type="passward"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              @click="login"
              :loading="loading"
              class="login_btn"
              type="primary"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url(../../assets/images/background.jpg) no-repeat;
  background-size: cover;
  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    padding: 40px;
    h1 {
      color: white;
      font-size: 40px;
    }
    h2 {
      font-size: 20px;
      color: white;
      margin: 20px 0px;
    }
    .login_btn {
      width: 100%;
    }
  }
}
</style>
